package com.hzyl.famousreader.view.fragment;


import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SPUtils;
import com.hzyl.famousreader.Constant;
import com.hzyl.famousreader.R;
import com.hzyl.famousreader.ThemeColor;
import com.hzyl.famousreader.databinding.FragmentUserBinding;
import com.hzyl.famousreader.router.Router;
import com.hzyl.famousreader.util.AppStoreUtils;
import com.hzyl.famousreader.viewmodel.MainViewModel;

/**
 * 用户
 */
@Route(path = "/main/user", name = "用户中心")
public class UserFragment extends BaseFragment {

  private FragmentUserBinding binding;

  private MainViewModel mainViewModel;

  private boolean isLogin = false;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);

    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_user, container, false);
    mainViewModel = ViewModelProviders.of(getActivity()).get(MainViewModel.class);

    int colorValue = SPUtils.getInstance().getInt(Constant.SP_THEME_COLOR,  ThemeColor.DEFAULT.getColor());
    BarUtils.setStatusBarColor(getActivity(), colorValue, 0);
    binding.toolbar.setBackgroundColor(colorValue);
    binding.userIvTheme.setBackgroundColor(colorValue);

    mainViewModel.themeColor.observe(getActivity(), new Observer<Integer>() {
      @Override
      public void onChanged(@Nullable Integer integer) {
        BarUtils.setStatusBarColor(getActivity(), colorValue, 0);
        binding.toolbar.setBackgroundColor(colorValue);
      }
    });

//    Glide.with(this).load(R.mipmap.img_guide_01).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(binding.iconAvatar);

//    String username = SPUtils.getInstance().getString(Constant.SP_USER_NAME, "登陆");
//    binding.userTvUsername.setText(username);
//
//    isLogin = SPUtils.getInstance().getBoolean(Constant.SP_IS_LOGIN, false);
//    if (!isLogin) {
//      binding.userBtnLogout.setVisibility(View.GONE);
//    }
//    binding.accountWrapper.setOnClickListener(v -> {
//      if(!isLogin) {
//        ARouter.getInstance().build("/login/login").navigation(getActivity());
//      }
//    });


    binding.commentWrapper.setOnClickListener(v -> AppStoreUtils.goAppStore());
    binding.feedbackWrapper.setOnClickListener(v -> {
      Uri uri = Uri.parse("mailto:591418197@qq.com");//指定协议，必须用mailto前缀声明，否则会唤起其他类似应用
      String[] email = {"591418197@qq.com"};
      Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
      intent.putExtra(Intent.EXTRA_CC, email);//抄送人
      intent.putExtra(Intent.EXTRA_SUBJECT, "问题反馈");//主题
      ActivityUtils.startActivity(intent);
    });
    binding.aboutWrapper.setOnClickListener(v -> ARouter.getInstance().build("/user/about").navigation());
//    binding.userBtnLogout.setOnClickListener(v -> {
//      isLogin = false;
//      binding.userBtnLogout.setVisibility(View.GONE);
//      SPUtils.getInstance().put(Constant.SP_IS_LOGIN, false);
//      SPUtils.getInstance().put(Constant.SP_USER_NAME, "登陆");
//      SPUtils.getInstance().remove(Constant.SP_REFRESH_TOKEN);
//      SPUtils.getInstance().remove(Constant.SP_ACCESS_TOKEN);
//      binding.userTvUsername.setText("登陆");
//    });
    binding.themeColorWrapper.setOnClickListener(v -> {
      ARouter.getInstance().build(Router.ACTIVITY_THEME).navigation(getActivity());
    });

    return binding.getRoot();
  }

}
